package com.xuexiang.xuidemo.provider;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.app.Context;

import com.xuexiang.xui_lib.util.UntilTNT;
import com.xuexiang.xuidemo.ResourceTable;

import java.util.List;

public class DropProvider extends BaseItemProvider {

    //private String TAG = "DropProvider";
    private List<UntilTNT> list = null;
    private Context context;
    private Color color, color2;

    public DropProvider(List<UntilTNT> list, Context context) {
        this.list = list;
        this.context = context;
        color = new Color(Color.getIntColor("#1CB8C3"));
        color2 = new Color(Color.getIntColor("#000000"));
    }

    //list的长度
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    //返回当前的item
    @Override
    public Object getItem(int i) {
        if (list != null && i >= 0 && i < list.size()) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        if (component == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_droprovider_view, componentContainer, false);
        } else {
            cpt = component;
        }
        UntilTNT untilTNT = list.get(i);

        Text text = (Text) cpt.findComponentById(ResourceTable.Id_textview);
        Image imgview = (Image) cpt.findComponentById(ResourceTable.Id_imgview);
        DependentLayout Dplayout = (DependentLayout) cpt.findComponentById(ResourceTable.Id_Dplayout);
        DependentLayout Dplayout1 = (DependentLayout) cpt.findComponentById(ResourceTable.Id_Dplayout1);
        Text textview1 = (Text) cpt.findComponentById(ResourceTable.Id_textview1);
        int type = untilTNT.getType();
        if (type == 0) { //有图片
            text.setText(untilTNT.getName());
            Dplayout.setVisibility(Component.VISIBLE);
            Dplayout1.setVisibility(Component.HIDE);
            if (i == untilTNT.getmSelectPosition()) {
                imgview.setVisibility(Component.VISIBLE);
                text.setTextColor(color);
            } else {
                imgview.setVisibility(Component.HIDE);
                text.setTextColor(color2);
            }
        } else {
            Dplayout.setVisibility(Component.HIDE);
            Dplayout1.setVisibility(Component.VISIBLE);
            textview1.setText(untilTNT.getName());
            if (i == untilTNT.getmSelectPosition()) {

                textview1.setTextColor(color);
            } else {

                textview1.setTextColor(color2);
            }
        }


        return cpt;
    }

}
